package com.fancymusic.controller;


import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.util.Util;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class AudioController {

    //设置APPID/AK/SK
    public static final String APP_ID = "22617423";
    public static final String API_KEY = "DGf84dwH2aXHdg2jkQe4MlDY";
    public static final String SECRET_KEY = "d2XcOwVEusf5mGhH80ruMZjQEH2yGtof";

    @PostMapping("/resolveAudio")
    public String resolveAudio(@RequestParam("file") MultipartFile multipartFile) throws IOException {

        AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
        client.setConnectionTimeoutInMillis(2000);
        byte[] bytes = multipartFile.getBytes();
        // 对语音二进制数据进行识别
//        String path = "G:\\CODE_PROJECTS\\A5_REVIEW_FIFTH_STAGE\\baidu-aip-speech\\src\\main\\resources\\test.pcm";
//        byte[] data = Util.readFileByBytes(path);     //readFileByBytes仅为获取二进制数据示例
        JSONObject asrRes2 = client.asr(bytes, "pcm", 16000, null);
        System.out.println(asrRes2);
        String result = asrRes2.get("result").toString();
        result = result.substring(result.indexOf('"')+1,result.lastIndexOf('。'));
        System.out.println(result);
        return result;

    }
}
